import requests

url = "http://10.19.151.208/vplatform/vmd/realplayUrlAlloc"
url_release = "http://10.19.151.208/vplatform/vmd/realplayUrlRelease"

deviceid_1 = 31000001451318000003

def get_URL(device_id):
    """
    get and return the play url by cam device_id with try/except ,
    return 0 when error occurs
    """
    d = {"requestid": "1", "token": "",
         "protocol": "http+flv", 'deviceid': device_id}
    try:
        r = requests.post(url, json=d)
        r = r.json()
        r_1 = r['playurl']
    except BaseException:
        print('==================================================================================')
        print('error! ==> get play url failed , with (url + device id) : (', url, "+", device_id, ')')
        print('error! ==> get play url failed , this cam may not on! ')
        print('==================================================================================')
        r_1 = 0
    return r_1


tobe_released = []


def url_release_function():
    for ur in tobe_released:
        if ur is not 0:
            d = {"requestid": "1", "token": "",
                 "protocol": "http+flv", 'playurl': ur}
            try:
                _r = requests.post(url, json=d)
            except BaseException:
                print('==================================================================================')
                print('error! ==> play url release failed , with url : (', url, ')')
                print('==================================================================================')